<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>08-留言版案例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            padding: 100px;
        }

        textarea {
            width: 200px;
            height: 100px;
            border: 1px solid skyblue;
            outline: none;
            resize: none;
        }

        ul {
            margin-top: 50px;
        }

        li {
            width: 300px;
            padding: 5px;
            background-color: rgb(245, 209, 243);
            color: red;
            font-size: 14px;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <textarea name="" id="" cols="30" rows="10"></textarea>
    <button>发布</button>
    <ul>

    </ul>
    <script>
        /* 
            需求：点击按钮，获取表单的值，创建li,把li追加到ul
        */
       var btn = document.querySelector('button');
       var ul = document.querySelector('ul');

       btn.onclick = function(){
           var value = document.querySelector('textarea').value;
           console.log(value);

           value = value.trim();
           if(value == ''){
                alert('不能为空');
                document.querySelector('textarea').value = '';

               // 在函数中，return之后的代码不再执行
               return;
           }
     
           var li = document.createElement('li');
           li.innerHTML = value;
           ul.appendChild(li);
           // 清空表单
           document.querySelector('textarea').value = '';
       }
    </script>
</body>
</html>